<?php include "classes.inc" ?>
<?php include "constants.inc" ?>
<?php session_start(); ?>
<?php

  if (isset($_GET['match'])) {
    $userInput = $_GET['match'];
  }
  
  if (isset($_GET['dep'])) {
    $dep = $_GET['dep'];
  }

  $connection = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
  mysql_select_db(DB_CATALOG);

  $result = mysql_query("select t.team_id, t.team_name from zt_calendar_pairing cp
    join zt_calendar c on cp.calendar_id = c.calendar_id join zt_team t
    on t.team_id = cp.home_team join club cl on cl.club_id = t.club_id
    where c.round_nr = ".$round." and t.group_id = ".$dep, $connection);
  

  $resultTypeItems = array();
	while ($row = mysql_fetch_assoc($result)) {
    if ( isset($userInput) && !empty($userInput) ) {
      if (substr_compare($row['team_name'], $userInput, 0, 1) === 0) {
        $keys[] = $row['team_id'];
        $values[] = $row['team_name'];
        $resultTypeItems = array_combine($keys, $values);
    	}
    } else {
      $keys[] = $row['team_id'];
      $values[] = $row['team_name'];
      $resultTypeItems = array_combine($keys, $values);
    }
	}

  echo json_encode($resultTypeItems);
?>